// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  const data = [];
  let mealList = await findAllMeal()
  for(let i = 0; i < mealList.list.length; i++){
    let meal = {}
      meal.id = mealList.list[i]._id
      meal.meal_name = mealList.list[i].meal_name
      meal.meal_num = mealList.list[i].meal_num
      meal.canteen_name = mealList.list[i].canteen[0].canteen_name
      meal.window_name = mealList.list[i].window[0].window_name
      meal.meal_img = mealList.list[i].meal_img
    data.push(meal)
  }
  return data;
}
async function findAllMeal (){
  return db.collection('meal').aggregate()
  .match({
    meal_status: false
  })
  .lookup({
    from: 'window',
    localField: 'window_id',
    foreignField: 'window_id',
    as: 'window'
  })
  .lookup({
    from: 'canteen',
    localField: 'canteen_id',
    foreignField: 'canteen_id',
    as: 'canteen'
  }).end()
  .then()
  .catch()
}
